package D12_9z.xml练习题.xml解析_5;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.List;
import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) throws Exception {
        Scanner sca = new Scanner(System.in);
        while (true) {
            System.out.println("-----------<查询>-----------");
            System.out.println("<1>查询出所有id大于等于2的所有图书信息");
            System.out.println("<2>查询出所有书名中包含Java的所有图书信息");
            System.out.println("<3>查询出所有价格大于50元的所有图书信息");
            System.out.println("<4>退出");
            int num = sca.nextInt();
            if (num == 1) {
                run1();

            }
            if (num == 2) {
                Object[] arr = new Object[]{"title", "Java"};
                run(arr);
            }
            if (num == 3) {
                run2();
            }
            if (num == 4) {
                return;
            }
        }
    }

    public static void run(Object[] arr) throws Exception {
        SAXReader sax = new SAXReader();
        Document read = sax.read("src/Y2019/M12_/D12_9/xml练习题/xml解析_5/book.xml");
        Element root = read.getRootElement();
        List<Element> book = root.elements("book");
        for (Element ele : book) {
            if (ele.elementText((String) arr[0]).contains((String) arr[1])) {
                System.out.println("====================================");
                System.out.println("id: " + ele.attributeValue("id"));
                System.out.println("title: " + ele.elementText("title"));
                System.out.println("publishDate: " + ele.elementText("publishDate"));
                System.out.println("price: " + ele.elementText("price"));
            }
        }
    }
    public static void run1() throws DocumentException {
        SAXReader sax = new SAXReader();
        Document read = sax.read("src/Y2019/M12_/D12_9/xml练习题/xml解析_5/book.xml");
        Element root = read.getRootElement();
        List<Element> book = root.elements("book");
        for (Element ele : book) {
            if (Integer.parseInt(ele.attributeValue("id")) >= 2) {
                System.out.println("====================================");
                System.out.println("id: " + ele.attributeValue("id"));
                System.out.println("title: " + ele.elementText("title"));
                System.out.println("publishDate: " + ele.elementText("publishDate"));
                System.out.println("price: " + ele.elementText("price"));
            }
        }
    }
    public static void run2()throws Exception{
        SAXReader sax = new SAXReader();
        Document read = sax.read("src/Y2019/M12_/D12_9/xml练习题/xml解析_5/book.xml");
        Element root = read.getRootElement();
        List<Element> book = root.elements("book");
        for (Element ele : book) {
            if (Integer.parseInt(ele.elementText("price")) > 50) {
                System.out.println("====================================");
                System.out.println("id: " + ele.attributeValue("id"));
                System.out.println("title: " + ele.elementText("title"));
                System.out.println("publishDate: " + ele.elementText("publishDate"));
                System.out.println("price: " + ele.elementText("price"));
            }
        }
    }
}

